package com.api.zszsj2.ZYT.util;

import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.NumberUtil;

import java.text.SimpleDateFormat;
import java.util.*;

public class SplitTimeInterval {



    private final  static  SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");

    /**
     * 把时间段拆分成以天为区分
     *
     * @return
     */
    public static List<Map<String, Object>> getDateDifference(String sd1, String sd2)  {
        List<Map<String, Object>> dateList = new ArrayList<>();

        try {
            Date d1 = sdf.parse(sd1);
            Date d2 = sdf.parse(sd2);
            // 获取两天间隔多少天，如果间隔大于一天计算，否则就返回当前时间
            int days = differentDays(d1, d2);
            if (days > 0) {
                for (int i = 0; i <= days; i++) {
                    Map<String, Object> dateMap = new HashMap<>();
                    Calendar calendar = Calendar.getInstance();
                    calendar.setTime(d1);
                    if (i==days){
                        calendar.setTime(d2);
                    }
                    calendar.add(Calendar.DATE, i);


                    Date startDate = getDayStart(calendar.getTime(), i==0);
                    Date endDate = getDayEnd(calendar.getTime(), i==days);
                    boolean isMidnight = isMidnight(endDate);

                    dateMap.put("startDate", sdf.format(startDate));
                    dateMap.put("endDate", sdf.format(endDate));
                    if (isMidnight){
                        dateMap.put("endDate", sdf.format(DateUtil.offsetDay(endDate, -1)).replace("00:00", "24:00"));
                    }
                    dateMap.put("duration", NumberUtil.decimalFormat("0.00", DateUtil.between(startDate, endDate, DateUnit.MINUTE) / 60.0));

                    dateList.add(dateMap);
                }
            } else {
                Map<String, Object> dateMap = new HashMap<>();
                dateMap.put("startDate", sd1);
                dateMap.put("endDate", sd2);
                dateMap.put("duration", NumberUtil.decimalFormat("0.00", DateUtil.between(d1, d2, DateUnit.MINUTE) / 60.0));

                dateList.add(dateMap);
            }
        }catch (Exception e){
            return null;
        }
        return dateList;
    }


    /**
     * date2比date1多的天数
     *
     * @param date1
     * @param date2
     * @return
     */
    public static int differentDays(Date date1, Date date2) {
        Calendar cal1 = Calendar.getInstance();
        cal1.setTime(date1);

        Calendar cal2 = Calendar.getInstance();
        cal2.setTime(date2);
        int day1 = cal1.get(Calendar.DAY_OF_YEAR);
        int day2 = cal2.get(Calendar.DAY_OF_YEAR);

        int year1 = cal1.get(Calendar.YEAR);
        int year2 = cal2.get(Calendar.YEAR);
        if (year1 != year2) //同一年
        {
            int timeDistance = 0;
            for (int i = year1; i < year2; i++) {
                if (i % 4 == 0 && i % 100 != 0 || i % 400 == 0) //闰年
                {
                    timeDistance += 366;
                } else //不是闰年
                {
                    timeDistance += 365;
                }
            }

            return (timeDistance + (day2 - day1) + 1);
        } else //不同年
        {
            System.out.println("判断day2 - day1 : " + (day2 - day1));
            return (day2 - day1);
        }
    }



    /**
     * 获取一天最开始的时间
     *
     * @param date
     * @return
     */
    public static Date getDayStart(Date date, boolean flag) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        if(flag){
            return calendar.getTime();
        }
        //设置每天的最小小时
        calendar.set(Calendar.HOUR_OF_DAY, calendar.getActualMinimum(Calendar.HOUR_OF_DAY));
        //设置每小时最小分钟
        calendar.set(Calendar.MINUTE, calendar.getActualMinimum(Calendar.MINUTE));
        //设置每分钟最小秒
        calendar.set(Calendar.SECOND, calendar.getActualMinimum(Calendar.SECOND));
        return calendar.getTime();
    }


    /**
     * 获取一天结束的时间
     *
     * @param date
     * @return
     */
    public static Date getDayEnd(Date date, boolean flag) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        if(flag){
            return calendar.getTime();
        }

        calendar.set(Calendar.HOUR_OF_DAY, 24);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }



    public static boolean isMidnight(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);

        return calendar.get(Calendar.HOUR_OF_DAY) == 0 &&
                calendar.get(Calendar.MINUTE) == 0 &&
                calendar.get(Calendar.SECOND) == 0 &&
                calendar.get(Calendar.MILLISECOND) == 0;
    }
}
